﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartMD.DataAccess.Interface.Dao;
using SmartMD.DataModel.Entity;

namespace SmartMD.DataAccess.Oracle.DaoImpl
{
    public class SpecRecordDaoImpl : BaseGenericDaoImpl
    {
        public bool DeleteObject(Object entity)
        {
            throw new NotImplementedException();
        }

        public bool SaveSpecUsages(List<SpecUsage> usagelist)
        {
            bool isSaved = true;
            SpecUsage record;
            for (int i = 0; i < usagelist.Count; i++)
            {
                record = usagelist[i];
                bool success = SaveSpecUsage(record);
                isSaved = isSaved & success;
            }
            return isSaved;
        }

        public bool SaveSpecUsage(SpecUsage usage)
        {
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" insert into");
            sb.Append(" DUO_SPEC_USAGE (RECORDID, USR, USINGTIME, PURPOSE)");
            sb.Append(" values(");
            sb.Append(" '" + usage.Id + "',");
            sb.Append(" '" + usage.User + "',");
            sb.Append(" to_date ('" + usage.UseTime.ToString("yyyy-MM-dd HH:mm:ss") + "' , 'YYYY-MM-DD HH24:MI:SS' ) ,");
            sb.Append(" '" + usage.Purpose + "'");

            sb.Append(")");
            bool success = this.dbConn.Insert(sb.ToString());
            return success;
        }
        /***
         * 功能：保存标本实体与使用记录关联关系
         * 
         * */
        public bool SaveEntityUsageMappings(List<EntityUsageMapping> mappingList)
        {
            if (mappingList == null)
                return false;
            bool isSaved = true;
            EntityUsageMapping mapping;
            for (int i = 0; i < mappingList.Count; i++)
            {
                mapping = mappingList[i];
                bool success = SaveEntityUsageMapping(mapping);
                isSaved = isSaved & success;
            }
            return isSaved;
        }
        public bool SaveEntityUsageMapping(EntityUsageMapping mapping)
        {
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" insert into");
            sb.Append(" DUO_ENTITYUSAGE_MAPPING (ID, ENTITYID, RECORDID)");
            sb.Append(" values(");
            sb.Append(" '" + mapping.Id + "',");
            sb.Append(" '" + mapping.EntityId + "',");
            sb.Append(" '" + mapping.RecordId + "'");

            sb.Append(")");
            bool success = this.dbConn.Insert(sb.ToString());
            return success;
        }

        public List<SpecUsage> loadAllSpecRecords()
        {
            throw new NotImplementedException();
        }

        public List<SpecUsage> loadSpecRecordById(string recordId)
        {
            throw new NotImplementedException();
        }
        public bool SaveOrUpdateObject(Object entity)
        {
            throw new NotImplementedException();
        }
    }
}
